using System;
using System.Collections.Generic;
using System.Text;

using MyAI.NeuralNetworkLib.Specification;

namespace MyAI.NeuralNetworkLib.Implementation
{
    public class LogisticSigmoid : IActivationFunction
    {
        #region Vars
        private List<double> _prms;
        #endregion

        #region Constructor
        public LogisticSigmoid()
        {
            _prms = new List<double>();
        }

        public LogisticSigmoid(double prm)
        {
            _prms = new List<double>();
            _prms[0] = prm;
        }
        #endregion

        #region IActivationFunction Members

        public double Calculate(double input)
        {
            if (_prms.Count < 1)
            {
                throw new InvalidOperationException("Not enough params");
            }
            return 1 / (1 + Math.Exp(-input * _prms[0]));
        }

        public double CalculateFirstDiff(double input)
        {
            if (_prms.Count < 1)
            {
                throw new InvalidOperationException("Not enough params");
            }
            return _prms[0] * Calculate(input) * (1 - Calculate(input));
        }

        public double CalculateSecondDiff(double input)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// LogisticSigmoid has only one parameter
        /// </summary>
        public void SetParams(List<double> prms)
        {
            _prms = prms;
        }

        public List<double> GetParams()
        {
            return _prms;
        }

        #endregion
    }
}
